Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Arjan H. 226 posts 463 karma points c-trib
    Mar 11, 2013 @ 20:45
    Arjan H.
    0

    Null reference error inside "if (HasValue) { }" statement?

    Can anyone explain why I'm getting a "Cannot perform runtime binding on a null reference" on this code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    if (Model.HasValue("visual")) {
    var image = Model.MediaById(Model.visual);
    <p>@Model.visual</p>
    <p>@image.Id</p>
    }
    }

    On the @image.Id call to be exact.
    But when I leave out the HasValue-check it works fine (I'm getting the same id twice, as expected):

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    var image = Model.MediaById(Model.visual);
    <p>@Model.visual</p>
    <p>@image.Id</p>
    }

    !?!?!?

    I'm using Umbraco 4.11.5

     

  • gary 385 posts 916 karma points
    Mar 11, 2013 @ 22:19
    gary
    0

    Hi Arjan

    Ran into this in V6 and am using DAMP, overcame it with

                            if (String.IsNullOrEmpty(Model.Visual.PropertyData))  ;

    This may not be exact for you, but you need to check against the string for an image.(you may need UmbracoFile??)

    As I say, this may not be a 100% answer, but will hopefully point you in the right direction.

    Regards G

     

     

     

  • Arjan H. 226 posts 463 karma points c-trib
    Mar 11, 2013 @ 23:36
    Arjan H.
    0

    I'm still not sure what caused the issue, but this fixed it for me:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
            if(Model.HasValue("visual")){
                    var image =Model.MediaById(Model.GetProperty("visual").Value);
                    <p>@Model.visual</p>
                    <p>@image.Id</p>
            }
    }

     

  • Francisco 21 posts 72 karma points
    Mar 12, 2013 @ 03:57
    Francisco
    0

    I believe Model.visual is not of type integer (required by .MediaById()).

    However, If you say that removing the condition, it works...

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 12, 2013 @ 05:29
    Fuji Kusaka
    0

    Hi Arjan,

    You are missing a @ sign in your code

    if(@Model.HasValue("visual")){
                   <p>@Model.visual</p>
                    <p>@image.Id</p>
            }
  • Arjan H. 226 posts 463 karma points c-trib
    Mar 12, 2013 @ 14:11
    Arjan H.
    0

    @Francisco: You're right, Model.visual (where "visual" is of type Media Picker) is not a string/integer, so it shouldn't be a valid parameter for the MediaById() method, but for some reason it works fine without the if-statement. Oh well, the GetProperty().Value approach works in both cases, so I'm happy. :)

    @Fuji: This is not true, if (Model.HasValue() { ... } works just fine, because we're already inside a @{ ... } statement.

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 16, 2013 @ 07:32
    Fuji Kusaka
    0

    Truue my bad!! :)

Please Sign in or register to post replies

Write your reply to:

Draft